﻿using BusinessLogic.GeneralService;
using BusinessLogic.Model;
using DataAccess.Domain;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApp.Controllers
{
    public class ProductController : Controller
    {
        IProductService productService;
        public ProductController()
        {
            productService = new ProductService();
        }
        public ActionResult Index()
        {
            return View(productService.GetList());
        }
       
        public ActionResult CreateNewProduct()
        {
            return View();
        }
        [HttpPost]
        public ActionResult CreateNewProduct(ProductAddNew productAddNew, HttpPostedFileBase ImageFile)
        {
            
            using (var ms = new MemoryStream())
            {
                ImageFile.InputStream.CopyTo(ms);
                productAddNew.Image = ms.ToArray();
            }

            if (ModelState.IsValid)
            {
                productService.AddNewProduct(productAddNew);
                return RedirectToAction("Index");
            }

            return View(productAddNew);
        }
        public FileContentResult LoadImageToHomepage(int id)
        {
            byte[] byteArray = productService.GetImageFromId(id);
            if (byteArray != null)
            {
                return new FileContentResult(byteArray, "image/jpeg");
            }
            else
            {
                return null;
            }
        }
        public ActionResult Detail(int id)
        {
            return View(productService.GetProductById(id));
        }

    }
}
